package com.satalyst.webtest.ui.framework;

import org.jdesktop.application.ApplicationContext;

import javax.swing.*;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Aidan Morgan
 */
public class Workspace extends JPanel {
    private ApplicationContext context;
    private List<WorkspaceEditor> workspaces;

    public Workspace(ApplicationContext context) {
        this.context = context;
        this.workspaces = new ArrayList<WorkspaceEditor>();
    }

    public void registerWorkspace(Class<? extends WorkspaceEditor> clazz) {
        try {
            WorkspaceEditor editor = clazz.getConstructor(ApplicationContext.class).newInstance(context);
            workspaces.add(editor);
        } catch (InstantiationException e) {
            throw new IllegalArgumentException(e);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException(e);
        } catch (InvocationTargetException e) {
            throw new IllegalArgumentException(e);
        } catch (NoSuchMethodException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public void activateWorkspace(Class workspaceClass) {
        for (WorkspaceEditor editor : workspaces) {
            if (editor.getClass().equals(workspaceClass)) {
                editor.activate(this);
                return;
            }
        }

        throw new IllegalArgumentException("Cannot find WorkspaceEditor class " + workspaceClass.getName() + ".");
    }
}
